package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.common.consts.RedisConst;
import com.atguigu.gmall.model.product.BaseCategory1;
import com.atguigu.gmall.model.product.BaseCategory2;
import com.atguigu.gmall.model.product.BaseCategory3;
import com.atguigu.gmall.model.product.BaseCategoryView;
import com.atguigu.gmall.model.to.CategoryTo;
import com.atguigu.gmall.product.mapper.Category1Mapper;
import com.atguigu.gmall.product.mapper.Category2Mapper;
import com.atguigu.gmall.product.mapper.Category3Mapper;
import com.atguigu.gmall.product.service.BaseCategoryService;
import com.atguigu.gmall.cache.service.CacheService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author 卡尼奶
 * @Date 2022/5/17 19:42
 * @Version 1.0
 */
@Service
public class BaseCategoryServiceImpl implements BaseCategoryService {

    @Autowired
    private Category1Mapper category1Mapper;

    @Autowired
    private Category2Mapper category2Mapper;

    @Autowired
    private Category3Mapper category3Mapper;

    @Autowired
    private CacheService cacheService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    private Map<String, Object> map = new ConcurrentHashMap<>();

    @Override
    public List<BaseCategory1> getCategory1() {
        return category1Mapper.selectList(null);
    }

    @Override
    public List<BaseCategory2> getCategoryByC1(Long category1Id) {
        QueryWrapper<BaseCategory2> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category1_id", category1Id);
        return category2Mapper.selectList(queryWrapper);
    }

    @Override
    public List<BaseCategory3> getCategoryByC2(Long category2Id) {
        QueryWrapper<BaseCategory3> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category2_id", category2Id);
        return category3Mapper.selectList(queryWrapper);
    }

    /*    @Override
        public List<CategoryTo> getAllCategory() {
            Object category = map.get("category");
            if (StringUtils.isEmpty(category)){
                List<CategoryTo> allCategory = category1Mapper.getAllCategory();
                map.put("category",allCategory);
                return allCategory;
            }
            return (List<CategoryTo>) category;
        }*/
    @Override
    public List<CategoryTo> getAllCategory() {
        List<CategoryTo> categoryToList = cacheService.getCache(RedisConst.CATEGORY_CACHE_KEY, new TypeReference<List<CategoryTo>>() {
        });
        if (StringUtils.isEmpty(categoryToList)) {
            categoryToList = category1Mapper.getAllCategory();
            cacheService.setCache(RedisConst.CATEGORY_CACHE_KEY, categoryToList);
            return categoryToList;
        }
        return categoryToList;
    }

    @Override
    public BaseCategoryView getSkuCategoryView(Long skuId) {
        return category1Mapper.getSkuCategoryView(skuId);
    }
}
